package whf.framework.report.export.view;

import whf.framework.datasource.DataRow;
import whf.framework.report.export.Export;
import whf.framework.report.export.ReportBuilder;
import whf.framework.web.tag.TableModel;
import whf.framework.web.tag.ext.IColumn;

/**
 * @author wanghaifeng
 * @create Dec 20, 2006 10:19:39 AM
 * 
 */
public class PdfView extends AbstractView {
	private StringBuffer xlsfo = new StringBuffer();
	
	public PdfView(ReportBuilder model, TableModel tableModel){
		super(model, tableModel);
	}

	public void beforeBody() {
		xlsfo.append(startRoot());
		xlsfo.append(regionBefore(model));
		xlsfo.append(regionAfter());
		xlsfo.append(columnDefinitions(model));
		xlsfo.append(header(model));
		xlsfo.append(" <fo:table-body> ");
	}

	public void doBody() {
		while(dataSource.hasNext()) {
			DataRow row = dataSource.next();
			for (int col = 0; col < this.columns.size(); col++) {
				IColumn column = this.columns.get(col);
				if (col == 0) {
					xlsfo.append(" <fo:table-row> ");
				}
				String value = row.getPropertyDisplay(column.getProperty());
				xlsfo.append(" <fo:table-cell border=\"solid silver .5px black \" display-align=\"center\" padding=\"3pt\"> ");
				xlsfo.append(" <fo:block   text-align=\"center\"  overflow=\"hidden\" font-family=\"arialuni,simsun,simhei\" >");
				xlsfo.append(value).append("</fo:block> ");
				xlsfo.append(" </fo:table-cell> ");
				if (col == (this.columns.size() - 1)) {
					xlsfo.append(" </fo:table-row> ");
				}
			}
		}
	}

	public Object afterBody() {
		xlsfo.append(" </fo:table-body> ");
		xlsfo.append(endRoot());
		return xlsfo.toString();
	}

	public String startRoot() {
		StringBuffer sb = new StringBuffer();

		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb.append("<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">");
		sb.append(" <fo:layout-master-set> ");
		sb.append(" <fo:simple-page-master master-name=\"simple\" ");
		sb.append(" page-height=\"11in\" ");
		sb.append(" page-width=\"8.5in\" ");
		sb.append(" margin-top=\".5in\" ");
		sb.append(" margin-bottom=\".25in\" ");
		sb.append(" margin-left=\".5in\" ");
		sb.append(" margin-right=\".5in\"> ");
		sb.append(" <fo:region-body margin-top=\".5in\" margin-bottom=\".25in\"/> ");
		sb.append(" <fo:region-before extent=\".5in\"/> ");
		sb.append(" <fo:region-after extent=\".25in\"/> ");
		sb.append(" </fo:simple-page-master> ");
		sb.append(" </fo:layout-master-set> ");
		sb.append(" <fo:page-sequence master-reference=\"simple\" language=\"zh\" initial-page-number=\"1\"> ");

		return sb.toString();
	}

	public String regionBefore(ReportBuilder model) {
		StringBuffer sb = new StringBuffer();

		Export export = model.getExportHandler().getCurrentExport();
		sb.append(" <fo:static-content flow-name=\"xsl-region-before\"> ");

		String title = export.getHeaderTitle();

		sb.append(" <fo:block space-after.optimum=\"15pt\" ");
		sb.append(" text-align=\"center\" font-size=\"17pt\" font-family=\"simhei\">");
		sb.append(title);
		sb.append("</fo:block> ");
		sb.append(" </fo:static-content> ");

		return sb.toString();
	}

	public String regionAfter() {
		StringBuffer sb = new StringBuffer();

		sb.append(" <fo:static-content flow-name=\"xsl-region-after\" display-align=\"after\"> ");

		sb.append(" <fo:block text-align=\"end\">Page <fo:page-number/></fo:block> ");

		sb.append(" </fo:static-content> ");

		return sb.toString();
	}

	public String columnDefinitions(ReportBuilder model) {
		StringBuffer sb = new StringBuffer();

		sb.append(" <fo:flow flow-name=\"xsl-region-body\"> ");
		sb.append(" <fo:block> ");
		sb.append(" <fo:table table-layout=\"fixed\" font-size=\"10pt\"> ");

		double columnCount = this.columns.size();
		double colwidth = 7.5 / columnCount;

		for (int i = 1; i <= columnCount; i++) {
			sb.append(" <fo:table-column column-number=\"" + i + "\" column-width=\"" + colwidth + "in\"/> ");
		}

		return sb.toString();
	}

	public String header(ReportBuilder model) {
		StringBuffer sb = new StringBuffer();

		Export export = model.getExportHandler().getCurrentExport();
		String headerColor = export.getHeaderColor();
		String headerBackgroundColor = export.getHeaderBackgroundColor();

		sb.append(" <fo:table-header background-color=\""
				+ headerBackgroundColor + "\" color=\"" + headerColor + "\"> ");

		sb.append(" <fo:table-row> ");
		for (IColumn tc: this.columns) {
			sb.append(" <fo:table-cell border=\"solid silver .5px\" text-align=\"center\" display-align=\"center\" padding=\"3pt\"> ");
			sb.append(" <fo:block  font-family=\"simhei\"  font-weight=\"bold\" >");
			sb.append(tc.getDisplayTitle());
			sb.append("</fo:block> ");
			sb.append(" </fo:table-cell> ");
		}
		sb.append(" </fo:table-row> ");

		sb.append(" </fo:table-header> ");

		return sb.toString();
	}

	public String endRoot() {
		StringBuffer sb = new StringBuffer();

		sb.append(" </fo:table> ");

		sb.append(" </fo:block> ");

		sb.append(" </fo:flow> ");

		sb.append(" </fo:page-sequence> ");

		sb.append(" </fo:root> ");

		return sb.toString();
	}

	public static void main(String[] args) throws Exception{
		//TTFReader.main(new String[]{"C:\\winnt\\fonts\\Arialuni.ttf","E:\\CVSRoot\\framework\\workspace2\\framework\\JavaSource\\conf\\arialuni.xml"});
		
	}
}
